﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.iss.inventory.Controllers;
using System.Drawing;

namespace Staionery_Inventory.Presentation.Admin
{
    public partial class DelegationUI__Reload_ : System.Web.UI.Page
    {
        public DelegationController delCon = new DelegationController();
        //used for delegate,get empName from masterpage login part

        public String empName = ""; public String headType = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            //security and assign name
            if (Session["empID"] == null)
            {
                Response.Redirect("~/Presentation/login.aspx");
            }

            Label lbl = this.Master.FindControl("lblLogInName") as Label;
            if (lbl != null)
            {
                lbl.Text = Session["UserName"].ToString();
            }
            //

            empName = (String)Session["UserName"];
            lblEmpRole.InnerText = delCon.getRoleByName(empName);
            lblHeadType.InnerText = delCon.getHeapTypeByName(empName);
            
            if (lblEmpRole.InnerText == "Employee") {
                lblDelegateTitle.Visible = false; lblForDelegateWork.Visible = false; lblDelegateWork.Visible = false;
                lblForDelegateEmp.Visible = false; txtDelegateEmp.Visible = false; btnSubmit.Visible = false;
                lblForDelegateFrom.Visible = false; txtDelegateFrom.Visible = false; btnDelegateFrom.Visible = false;
                lblForDelegateTill.Visible = false; txtDelegateTill.Visible = false; btnDelegateTill.Visible = false;
                lblDelegatedTitle.Visible = true; lblDelegatedMessage.Visible = true;
                lblForDelegatedWork.Visible = true; lblDelegatedWork.Visible = true;
                lblForDelegatedBy.Visible = true; lblDelegatedBy.Visible = true;
                lblForDelegatedFrom.Visible = true; lblDeleagtedFrom.Visible = true;
                lblForDelegatedTill.Visible = true; lblDelegatedTill.Visible = true;
            }
            else if (lblEmpRole.InnerText == "Head")
            {
                if (lblHeadType.InnerText == "Manager") {
                    lblDelegateTitle.Visible = true; lblForDelegateWork.Visible = true; lblDelegateWork.Visible = true;
                    lblForDelegateEmp.Visible = true; txtDelegateEmp.Visible = true; btnSubmit.Visible = true;
                    lblForDelegateFrom.Visible = true; txtDelegateFrom.Visible = true; btnDelegateFrom.Visible = true;
                    lblForDelegateTill.Visible = true; txtDelegateTill.Visible = true; btnDelegateTill.Visible = true;
                    lblDelegatedTitle.Visible = true; lblDelegatedMessage.Visible = true;
                    lblForDelegatedWork.Visible = true; lblDelegatedWork.Visible = true;
                    lblForDelegatedBy.Visible = true; lblDelegatedBy.Visible = true;
                    lblForDelegatedFrom.Visible = true; lblDeleagtedFrom.Visible = true;
                    lblForDelegatedTill.Visible = true; lblDelegatedTill.Visible = true;
                    if (Session["empID"] == Session["DeleEmployeeID"]) {
                        btnSubmit.Visible = false; btnDelegateFrom.Visible = false; btnDelegateTill.Visible = false;
                    }
                }
                else if (lblHeadType.InnerText == "Head") {
                    lblDelegateTitle.Visible = true; lblForDelegateWork.Visible = true; lblDelegateWork.Visible = true;
                    lblForDelegateEmp.Visible = true; txtDelegateEmp.Visible = true; btnSubmit.Visible = true;
                    lblForDelegateFrom.Visible = true; txtDelegateFrom.Visible = true; btnDelegateFrom.Visible = true;
                    lblForDelegateTill.Visible = true; txtDelegateTill.Visible = true; btnDelegateTill.Visible = true;
                    lblDelegatedTitle.Visible = false; lblDelegatedMessage.Visible = false;
                    lblForDelegatedWork.Visible = false; lblDelegatedWork.Visible = false;
                    lblForDelegatedBy.Visible = false; lblDelegatedBy.Visible = false;
                    lblForDelegatedFrom.Visible = false; lblDeleagtedFrom.Visible = false;
                    lblForDelegatedTill.Visible = false; lblDelegatedTill.Visible = false;
                    if (Session["empID"] == Session["DeleEmployeeID"])
                    {
                        btnSubmit.Visible = false; btnDelegateFrom.Visible = false; btnDelegateTill.Visible = false;
                    }
                }
            }
            else if (lblEmpRole.InnerText == "Supervisor")
            {
                lblDelegateTitle.Visible = true; lblForDelegateWork.Visible = true; lblDelegateWork.Visible = true;
                lblForDelegateEmp.Visible = true; txtDelegateEmp.Visible = true; btnSubmit.Visible = true;
                lblForDelegateFrom.Visible = true; txtDelegateFrom.Visible = true; btnDelegateFrom.Visible = true;
                lblForDelegateTill.Visible = true; txtDelegateTill.Visible = true; btnDelegateTill.Visible = true;
                lblDelegatedTitle.Visible = true; lblDelegatedMessage.Visible = true;
                lblForDelegatedWork.Visible = true; lblDelegatedWork.Visible = true;
                lblForDelegatedBy.Visible = true; lblDelegatedBy.Visible = true;
                lblForDelegatedFrom.Visible = true; lblDeleagtedFrom.Visible = true;
                lblForDelegatedTill.Visible = true; lblDelegatedTill.Visible = true;
                if (Session["empID"] == Session["DeleEmployeeID"])
                {
                    btnSubmit.Visible = false; btnDelegateFrom.Visible = false; btnDelegateTill.Visible = false;
                }
            }

            //Delegate Section
            String[] ee = delCon.getDelegateByName(empName);
            if (ee != null)
            {
                lblDelegatedWork.Text = ee[0]; lblDelegatedBy.Text = ee[1]; lblDeleagtedFrom.Text = ee[2]; lblDelegatedTill.Text = ee[3];
                lblDelegatedMessage.InnerText = "There is a delegate work now";
            }
            else { lblDelegatedMessage.InnerText = "There is no delegate work now"; }
            //Delegator Section
            String[] er = delCon.getDelegatorByName(empName);
            if (!IsPostBack)
            {
                if (er[1] != "")
                {
                    btnDelegateFrom.Enabled = false; btnDelegateTill.Enabled = false;
                    lblDelegateWork.Text = er[0]; txtDelegateEmp.Text = er[1]; txtDelegateFrom.Text = er[2]; txtDelegateTill.Text = er[3];
                    txtDelegateEmp.ReadOnly = true; txtDelegateFrom.ReadOnly = true; txtDelegateTill.ReadOnly = true; btnSubmit.Text = "Cancel";
                }
                else
                {
                    btnDelegateFrom.Enabled = true; btnDelegateTill.Enabled = true;
                    lblDelegateWork.Text = er[0];
                    txtDelegateEmp.ReadOnly = false; txtDelegateFrom.ReadOnly = true; txtDelegateTill.ReadOnly = true; btnSubmit.Text = "Submit";
                }
            }
        }

        protected void btnDelegateFrom_Click(object sender, ImageClickEventArgs e)
        {
            clDelegateFrom.Visible = true;
        }

        protected void clDelegateFrom_SelectionChanged(object sender, EventArgs e)
        {
            if (clDelegateFrom.SelectedDate < DateTime.Now)
            {
                lblMessage.ForeColor = Color.Red;
                lblMessage.Text = "You can't select a date before today";
            }
            else {
                txtDelegateFrom.Text = clDelegateFrom.SelectedDate.ToShortDateString();
                clDelegateFrom.Visible = false;
                lblMessage.Text = "";
            }
        }

        protected void btnDelegateTill_Click(object sender, ImageClickEventArgs e)
        {
            clDelegateTill.Visible = true;
        }

        protected void clDelegateTill_SelectionChanged(object sender, EventArgs e)
        {
            if (clDelegateTill.SelectedDate < clDelegateFrom.SelectedDate)
            {
                lblMessage.ForeColor = Color.Red;
                lblMessage.Text = "You can't select a date before the delegation start date";
            }
            else {
                txtDelegateTill.Text = clDelegateTill.SelectedDate.ToShortDateString();
                clDelegateTill.Visible = false;
                lblMessage.Text = "";
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (btnSubmit.Text == "Submit")
            {
                if ((txtDelegateFrom.Text == "") || (txtDelegateTill.Text == ""))
                {
                    lblMessage.ForeColor = Color.Red;
                    lblMessage.Text = "Please select the delegation start date and end date";
                }
                else {
                    lblMessage.Text = delCon.SubmitDelegate(empName, txtDelegateEmp.Text, txtDelegateFrom.Text, txtDelegateTill.Text);
                    if (lblMessage.Text == "Delegate Successfully")
                    {
                        btnDelegateFrom.Enabled = false; btnDelegateTill.Enabled = false;
                        lblMessage.ForeColor = Color.Black;
                        txtDelegateEmp.ReadOnly = true; btnSubmit.Text = "Cancel";
                    }
                    else { lblMessage.ForeColor = Color.Red; }
                }

            }
            else if (btnSubmit.Text == "Cancel")
            {
                btnDelegateFrom.Enabled = true; btnDelegateTill.Enabled = true;
                lblMessage.ForeColor = Color.Black;
                lblMessage.Text = delCon.CancelDelegate(empName);
                txtDelegateEmp.ReadOnly = false; btnSubmit.Text = "Submit";
                clearDelegatorFields();
            }
        }

        public void clearDelegationFields()
        {
            lblDelegatedWork.Text = ""; lblDelegatedBy.Text = ""; lblDeleagtedFrom.Text = ""; lblDelegatedTill.Text = "";
        }

        public void clearDelegatorFields()
        {
            txtDelegateEmp.Text = ""; txtDelegateFrom.Text = ""; txtDelegateTill.Text = "";
        }

    }
}